﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BasePlatformNet
{
	// 数据包装类 - 用于解析 Json 格式的布线文件
	public class AddrMapJson
	{
		public List<AddrMapJsonUnit> map;		// Json中存储的基础数据

		/*********************** 统计数据 ***************************/

		public int subNum = 0;					// 控制器个数

		public int lampNum = 0;					// 有效灯个数

		public List<SubInfo4Json> subInfoList = new List<SubInfo4Json>();

		public int maxLampNum_TotalPort = 0;	// 控制器所有端口最大灯点个数

		/*********************** 统计数据 ***************************/

		public bool collectSubInfo()
		{
			if (map == null || map.Count == 0)
			{
				return true;
			}

			try
			{
				for (int i = 0; i < map.Count; i++)
				{
					AddrMapJsonUnit mapUnit = map[i];
					int currentIndex = int.Parse(mapUnit.Sub);
					int position = existIndex(currentIndex);
					// 发现新编号的分控数据
					if (position == -1)
					{
						SubInfo4Json subInfo = new SubInfo4Json();
						subInfo.index = currentIndex;
						subInfo.updatePortAndLamp(mapUnit);
						subInfoList.Add(subInfo);
					}
					// 已解析过相同 index(Sub) 的分控
					else
					{
						subInfoList[position].updatePortAndLamp(mapUnit);
					}
				}

				// 计算统计信息
				subNum = subInfoList.Count;
				int totalPort_tmp = 0;
				foreach (SubInfo4Json subInfo in subInfoList)
				{
					subInfo.updateStatisticsInfo();
					lampNum += subInfo.totalLampNum;
					if (totalPort_tmp < subInfo.maxLampNum_SinglePort)
					{
						totalPort_tmp = subInfo.maxLampNum_SinglePort;
					}
				}
				maxLampNum_TotalPort = totalPort_tmp;
			}
			catch (Exception ex)
			{
				Console.WriteLine(ex.Message + ex.StackTrace);
				return false;
			}
			
			return true;
		}

		private int existIndex(int index)
		{
			for(int i = 0; i < subInfoList.Count; i++)
			{
				SubInfo4Json subInfo = subInfoList[i];
				if(index == subInfo.index)
				{
					return i;
				}
			}

			return -1;
		}
	}

	public class AddrMapJsonUnit
	{
		public string Sub;

		public string port;

		public string lamp;

		public string x;

		public string y;

		public string type;
	}

	public class SubInfo4Json
	{
		public int index = 0;

		public int maxPortNum = 0;				// 控制器最大端口个数

		public int maxLampNum_SinglePort = 0;	// 控制器单个端口最大灯点个数

		public int totalLampNum = 0;			// 控制器所有端口灯点和

		public List<PortInfo4Json> portList = new List<PortInfo4Json>();

		// 根据 mapUnit 中的信息更新自身信息
		public void updatePortAndLamp(AddrMapJsonUnit mapUnit)
		{
			int currentPortIndex = int.Parse(mapUnit.port);

			bool addFlag = true;
			for (int i = 0; i < portList.Count; i++)
			{
				if (portList[i].index == currentPortIndex)
				{
					portList[i].updateLampInfo(mapUnit);
					addFlag = false;
				}
			}
			if (addFlag == true)
			{
				PortInfo4Json port = new PortInfo4Json();
				port.index = currentPortIndex;
				port.updateLampInfo(mapUnit);
				portList.Add(port);
			}
		}

		// 计算统计信息
		public void updateStatisticsInfo()
		{
			maxPortNum = portList.Count;
			int tmpMaxPortLampNum = 0;
			foreach (PortInfo4Json port in portList)
			{
				if (tmpMaxPortLampNum < port.lampList.Count)
				{
					tmpMaxPortLampNum = port.lampList.Count;
				}
				totalLampNum += port.lampList.Count;
			}
			maxLampNum_SinglePort = tmpMaxPortLampNum;
		}
	}

	public class PortInfo4Json
	{
		public int index;

		public List<LampInfo4Json> lampList = new List<LampInfo4Json>();

		public void updateLampInfo(AddrMapJsonUnit mapUnit)
		{
			int cx = int.Parse(mapUnit.x);
			int cy = int.Parse(mapUnit.y);
			lampList.Add(new LampInfo4Json(cx, cy, mapUnit.type));
		}
	}

	public class LampInfo4Json
	{
		public int x;

		public int y;

		public string type;

		public LampInfo4Json(int x, int y, string type)
		{
			this.x = x;

			this.y = y;

			this.type = type;
		}
	}
}
